﻿using System;
using System.Text.RegularExpressions;

namespace StartReading.Security.Validation
{
    class RegexValidation : IValidation<String, String, String>
    {
        private RegexValidation()
        {
        }

        #region Lazy Singleton Thread-Safe
        private static class RegexValidationHolder
        {
            static RegexValidationHolder()
            {
            }

            internal static readonly RegexValidation RegexValidation = new RegexValidation();
        }

        /// <summary>
        /// Gets an instance of RegexValidation class
        /// </summary>
        public static RegexValidation Instance
        {
            get { return RegexValidationHolder.RegexValidation; }
        }
        #endregion

        /// <summary>
        ///     Validates if item matches the given pattern.
        /// </summary>
        /// <param name="item">
        ///     String to be validated
        /// </param>
        /// <param name="pattern">
        ///     String to be matched against with item String
        /// </param>
        /// <param name="exceptionMessage">
        ///     An exception message if item does not match the pattern
        /// </param>
        /// <exception cref="System.ArgumentException"></exception>
        public void IsValid( string item, string pattern, string exceptionMessage )
        {
            var matches = Regex.Matches( item.ToLower(), pattern );

            if ( matches.Count == 0 )
                throw new ArgumentException( exceptionMessage );            
        }
    }
}
